﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace UFIDA.U9.Cust.STR.PlugBE.Public
{
    public static class PublicMethod
    {

        /// <summary>
        /// 将字符数组添加单引号并用逗号分割，用于sql查询
        /// </summary>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string StringJoin(IEnumerable<string> args)
        {
            return string.Join(",", args.Select(t => $"'{t}'"));
        }

        /// <summary>  
        /// 计算字符串的MD5哈希值  
        /// </summary>  
        /// <param name="input">待计算的字符串</param>  
        /// <returns>MD5哈希值的十六进制字符串表示</returns>  
        public static string ComputeMD5Hash(string input)
        {
            // 创建一个MD5CryptoServiceProvider对象  
            using (MD5 md5Hash = MD5.Create())
            {
                // 将输入字符串转换为字节数组，并计算哈希数据  
                byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));

                // 创建一个StringBuilder来收集字节  
                // 并将其转换为十六进制字符串  
                StringBuilder sBuilder = new StringBuilder();

                // 循环遍历哈希数据的每个字节  
                // 并格式化为两位数的十六进制数  
                for (int i = 0; i < data.Length; i++)
                {
                    sBuilder.Append(data[i].ToString("x2"));
                }

                // 返回十六进制字符串  
                return sBuilder.ToString();
            }
        }
    }
}
